package com.back.demo.controller;

import com.back.demo.common.CommonResult;
import com.back.demo.controller.myVO.basicVO.StudentInformationVO;
import com.back.demo.controller.myVO.studentVO.*;
import com.back.demo.controller.myVO.studentVO.achievementVO.*;
import com.back.demo.service.StudentService;
import com.back.demo.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Api(tags = "学生功能")
@RestController
@RequestMapping("/server/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;


    // 查询学生个人信息
    // 查询学生个人信息
    // 查询学生个人信息
    // 查询学生个人信息
    // 查询学生个人信息
    @ApiOperation("学生个人信息查询接口")
    @GetMapping("/get-information")
    public CommonResult<?> getStudentInfo(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回学生个人信息
        StudentInformationVO studentInformation = studentService.findStudentInformation(studentId);
        return CommonResult.success(studentInformation);
    }
    // 查询学生个人信息
    // 查询学生个人信息
    // 查询学生个人信息
    // 查询学生个人信息
    // 查询学生个人信息


    // acquire
    // acquire
    // acquire
    // acquire
    // acquire
    @ApiOperation("学生所有论文成果的查询接口")
    @GetMapping("/acquire-paper")
    public CommonResult<?> acquirePaper(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回该学生所有论文成果
        ArrayList<ItemStudentVO> listVO = studentService.acquirePaper(studentId);
        return CommonResult.success(listVO);
    }

    @ApiOperation("学生所有专利成果的查询接口")
    @GetMapping("/acquire-patent")
    public CommonResult<?> acquirePatent(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回该学生所有专利成果
        ArrayList<ItemStudentVO> listVO = studentService.acquirePatent(studentId);
        return CommonResult.success(listVO);
    }

    @ApiOperation("学生所有比赛成果的查询接口")
    @GetMapping("/acquire-competition")
    public CommonResult<?> acquireCompetition(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回该学生所有专利成果
        ArrayList<ItemStudentVO> listVO = studentService.acquireCompetition(studentId);
        return CommonResult.success(listVO);
    }

    @ApiOperation("学生所有软件著作权成果的查询接口")
    @GetMapping("/acquire-software-copyright")
    public CommonResult<?> acquireSoftwareCopyright(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回该学生所有专利成果
        ArrayList<ItemStudentVO> listVO = studentService.acquireSoftwareCopyright(studentId);
        return CommonResult.success(listVO);
    }

    @ApiOperation("学生所有其它成果的查询接口")
    @GetMapping("/acquire-other")
    public CommonResult<?> acquireOther(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回该学生所有专利成果
        ArrayList<ItemStudentVO> listVO = studentService.acquireOther(studentId);
        return CommonResult.success(listVO);
    }
    // acquire
    // acquire
    // acquire
    // acquire
    // acquire


    // add
    // add
    // add
    // add
    // add
    @ApiOperation("学生论文成果提交接口")
    @PostMapping("/submit-paper")
    public CommonResult<?> submitPaper(@RequestHeader("Authorization") String authHeader, @RequestBody PaperVO paperVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        if (paperVO.getPaperId() == 0) { // 表示新增，并处理插入失败的情况
            try {
                studentService.addPaper(studentId, paperVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        } else { // 表示修改，并处理修改失败的情况
            try {
                studentService.modifyPaper(studentId, paperVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        }
        return CommonResult.success("操作成功");
    }

    @ApiOperation("学生专利成果提交接口")
    @PostMapping("/submit-patent")
    public CommonResult<?> submitPatent(@RequestHeader("Authorization") String authHeader, @RequestBody PatentVO patentVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        if (patentVO.getPatentId() == 0) { // 表示新增，并处理插入失败的情况
            try {
                studentService.addPatent(studentId, patentVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        } else { // 表示修改，并处理修改失败的情况
            try {
                studentService.modifyPatent(studentId, patentVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        }
        return CommonResult.success("提交成功");
    }

    @ApiOperation("学生比赛成果提交接口")
    @PostMapping("/submit-competition")
    public CommonResult<?> submitCompetition(@RequestHeader("Authorization") String authHeader, @RequestBody CompetitionVO competitionVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        if (competitionVO.getCompetitionId() == 0) { // 表示新增，并处理插入失败的情况
            try {
                studentService.addCompetition(studentId, competitionVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        } else { // 表示修改，并处理修改失败的情况
            try {
                studentService.modifyCompetition(studentId, competitionVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        }
        return CommonResult.success("提交成功");
    }

    @ApiOperation("学生软件著作权成果提交接口")
    @PostMapping("/submit-software-copyright")
    public CommonResult<?> submitSoftwareCopyright(@RequestHeader("Authorization") String authHeader, @RequestBody SoftwareCopyrightVO softwareCopyrightVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        if (softwareCopyrightVO.getSoftwareCopyrightId() == 0) { // 表示新增，并处理插入失败的情况
            try {
                studentService.addSoftwareCopyright(studentId, softwareCopyrightVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        } else { // 表示修改，并处理修改失败的情况
            try {
                studentService.modifySoftwareCopyright(studentId, softwareCopyrightVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        }
        return CommonResult.success("提交成功");
    }

    @ApiOperation("学生其它成果提交接口")
    @PostMapping("/submit-other")
    public CommonResult<?> submitOther(@RequestHeader("Authorization") String authHeader, @RequestBody OtherVO otherVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        if (otherVO.getOtherId() == 0) { // 表示新增，并处理插入失败的情况
            try {
                studentService.addOther(studentId, otherVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        } else { // 表示修改，并处理修改失败的情况
            try {
                studentService.modifyOther(studentId, otherVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        }
        return CommonResult.success("提交成功");
    }
    // add
    // add
    // add
    // add
    // add

    // find
    // find
    // find
    // find
    // find
    @ApiOperation("论文详情查找接口")
    @GetMapping("/find-paper-detail/{id}")
    public CommonResult<?> findPaperDetail(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

//        System.out.println(studentId);
//        System.out.println(id);
        try {
            return CommonResult.success(studentService.findPaperDetail(studentId, id));
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "未能成功获取论文详情");
        }

    }

    @ApiOperation("专利详情查找接口")
    @GetMapping("/find-patent-detail/{id}")
    public CommonResult<?> findPatentDetail(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            return CommonResult.success(studentService.findPatentDetail(studentId, id));
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "未能成功获取专利成果的详情");
        }

    }

    @ApiOperation("比赛详情查找接口")
    @GetMapping("/find-competition-detail/{id}")
    public CommonResult<?> findCompetitionDetail(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            return CommonResult.success(studentService.findCompetitionDetail(studentId, id));
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "未能成功获取比赛成果的详情");
        }

    }

    @ApiOperation("软件著作权详情查找接口")
    @GetMapping("/find-software-copyright-detail/{id}")
    public CommonResult<?> findSoftwareCopyrightDetail(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            return CommonResult.success(studentService.findSoftwareCopyrightDetail(studentId, id));
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "未能成功获取软件著作权成果的详情");
        }
    }

    @ApiOperation("其它详情查找接口")
    @GetMapping("/find-other-detail/{id}")
    public CommonResult<?> findOtherDetail(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            return CommonResult.success(studentService.findOtherDetail(studentId, id));
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "未能成功获取其它成果的详情");
        }
    }
    // find
    // find
    // find
    // find
    // find

    // remove
    // remove
    // remove
    // remove
    // remove
    @ApiOperation("删除论文接口")
    @DeleteMapping("/remove-paper/{id}")
    public CommonResult<?> removePaper(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            studentService.removePaper(studentId, id);
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "删除失败");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("删除专利接口")
    @DeleteMapping("/remove-patent/{id}")
    public CommonResult<?> removePatent(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            studentService.removePatent(studentId, id);
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "删除失败");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("删除比赛接口")
    @DeleteMapping("/remove-competition/{id}")
    public CommonResult<?> removeCompetition(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            studentService.removeCompetition(studentId, id);
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "删除失败");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("删除软件著作权接口")
    @DeleteMapping("/remove-software-copyright/{id}")
    public CommonResult<?> removeSoftwareCopyright(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            studentService.removeSoftwareCopyright(studentId, id);
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "删除失败");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("删除其它接口")
    @DeleteMapping("/remove-other/{id}")
    public CommonResult<?> removeOther(@RequestHeader("Authorization") String authHeader, @PathVariable Integer id) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);
        try {
            studentService.removeOther(studentId, id);
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "删除失败");
        }
        return CommonResult.success("删除成功");
    }
    // remove
    // remove
    // remove
    // remove
    // remove

    // upload-download
    // upload-download
    // upload-download
    // upload-download
    // upload-download
    @ApiOperation("上传文件")
    @PostMapping("/upload-file")
    public CommonResult<?> uploadFile(@RequestHeader("Authorization") String authHeader, @RequestParam("file") MultipartFile file, @RequestParam("type") Integer type, @RequestParam("id") Integer id) throws IOException {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 查找科研详情，判断该成果是否属于该学生，以免出现非法操作
        switch (type) {
            case 1:
                PaperVO paperDetail = studentService.findPaperDetail(studentId, id);
                if (paperDetail == null) {
                    return CommonResult.error(50005, "非法操作！");
                }
                break;
            case 2:
                PatentVO patentDetail = studentService.findPatentDetail(studentId, id);
                if (patentDetail == null) {
                    return CommonResult.error(50005, "非法操作！");
                }
                break;
            case 3:
                CompetitionVO competitionDetail = studentService.findCompetitionDetail(studentId, id);
                if (competitionDetail == null) {
                    return CommonResult.error(50005, "非法操作！");
                }
                break;
            case 4:
                SoftwareCopyrightVO softwareCopyrightDetail = studentService.findSoftwareCopyrightDetail(studentId, id);
                if (softwareCopyrightDetail == null) {
                    return CommonResult.error(50005, "非法操作！");
                }
                break;
            case 5:
                OtherVO otherDetail = studentService.findOtherDetail(studentId, id);
                if (otherDetail == null) {
                    return CommonResult.error(50005, "非法操作！");
                }
        }

        String information = studentService.upload(file, type, id);
        if (information.equals("文件上传成功")) {
            return CommonResult.success(information);
        }
        return CommonResult.error(50001, information);
    }

    @ApiOperation("下载文件")
    @PostMapping("/download-file")
    public void downloadFile(@RequestHeader("Authorization") String authHeader, @RequestParam("type") Integer type, @RequestParam("id") Integer id, HttpServletResponse response) throws IOException {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 查找科研详情，判断该成果是否属于该学生，以免出现非法操作
        switch (type) {
            case 1:
                PaperVO paperDetail = studentService.findPaperDetail(studentId, id);
                if (paperDetail == null) {
                    return;
                }
                break;
            case 2:
                PatentVO patentDetail = studentService.findPatentDetail(studentId, id);
                if (patentDetail == null) {
                    return;
                }
                break;
            case 3:
                CompetitionVO competitionDetail = studentService.findCompetitionDetail(studentId, id);
                if (competitionDetail == null) {
                    return;
                }
                break;
            case 4:
                SoftwareCopyrightVO softwareCopyrightDetail = studentService.findSoftwareCopyrightDetail(studentId, id);
                if (softwareCopyrightDetail == null) {
                    return;
                }
                break;
        }

        studentService.download(studentId, type, id, response);
    }
    // upload-download
    // upload-download
    // upload-download
    // upload-download
    // upload-download
}
